﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using JetBrains.Annotations;

/// <summary>
/// 把Resource下的资源打包成.data到OutPut目录下
/// </summary>
public class BoundleBuilder : Editor
{
    public class AssetBundleImporter : AssetPostprocessor
    {

        //// 全部资源加载完毕之后调用
        //static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        //{
        //    foreach (var item in importedAssets)
        //    {
        //          var importer = AssetImporter.GetAtPath(item);
        //          importer.assetBundleName = "data";
        //    }
        //}
    }
    public class CreateAssetBundles
    {
        //在Unity编辑器中添加菜单
        [MenuItem("NL-Tools/资产打包(AB)/打包 IOS(多选)")]
        static void ExportResource_IOS()
        {
            _BuildAssetBundleBySelection(BuildTarget.iOS);
        }
        [MenuItem("NL-Tools/资产打包(AB)/打包 Android(多选)")]
        static void ExportResource_Android()
        {
            _BuildAssetBundleBySelection(BuildTarget.Android); 
        }

        //在Unity编辑器中添加菜单
        [MenuItem("NL-Tools/资产打包(AB)/打包 WebGL(多选)")]
        static void ExportResource_WebGL()
        {
            _BuildAssetBundleBySelection(BuildTarget.WebGL);
        }


        static void _BuildAssetBundleBySelection(BuildTarget buildtarget)
        {
            if (Selection.objects == null || Selection.objects.Length < 1)
                return;

            

            string dir = $"{Application.dataPath}/../MAB_AssetBundle_{buildtarget}";

            //判断该目录是否存在
            if (Directory.Exists(dir) == false)
            {
                Directory.CreateDirectory(dir);//在工程下创建AssetBundles目录
            }

            int count = Selection.count;
            for (int i = 0; i < count; i++)
            {
                var obj = Selection.objects[i];
                //备打文件集合，当前为单个
                AssetBundleBuild[] abb = new AssetBundleBuild[1];
                //文件路径（当前为单个，多个文件使用数组的形式）
                string[] path = new string[1];
                //获取文件路径（得到鼠标选中的文件）；
                path[0] = AssetDatabase.GetAssetPath(obj);
                //备打文件路径名，从Asset/开始
                abb[0].assetNames = path;
                //包名
                abb[0].assetBundleName = obj.name;
                abb[0].assetBundleVariant = "mab";

                //打包（“输出路径”，备打文件集合，打包设置，目标平台）
                BuildPipeline.BuildAssetBundles(dir, abb, BuildAssetBundleOptions.DeterministicAssetBundle, buildtarget);

            }
            EditorUtility.RevealInFinder(dir);
            //EditorUtility.OpenFolderPanel("",dir,"");
            AssetDatabase.Refresh();
        }

        //[MenuItem("菜单/资源打包(ALL)")]
        //static void BuildAllExAssetBundles()//进行打包
        //{
        //    string dir = Application.streamingAssetsPath + "../..//MAB_AssetBundle";// "AssetBundles";
        //                                                            //判断该目录是否存在
        //    if (Directory.Exists(dir) == false)
        //    {
        //        Directory.CreateDirectory(dir);//在工程下创建AssetBundles目录
        //    }

        //    BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
        //    //刷新
        //    AssetDatabase.Refresh();
        //}
        //[MenuItem("菜单/资源打包_Android")]
        //static void BuildAllExAssetBundles_Android()//进行打包
        //{
        //    string dir = Application.streamingAssetsPath + "../..//MAB_AssetBundle_Android";// "AssetBundles";
        //                                                                //判断该目录是否存在
        //    if (Directory.Exists(dir) == false)
        //    {
        //        Directory.CreateDirectory(dir);//在工程下创建AssetBundles目录
        //    }

        //    BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.Android);
        //    //刷新
        //    AssetDatabase.Refresh();
        //}
        //[MenuItem("菜单/资源打包_IOS")]
        //static void BuildAllExAssetBundles_IOS()//进行打包
        //{
        //    string dir = Application.streamingAssetsPath + "../..//MAB_AssetBundle_IOS";// "AssetBundles";
        //                                                            //判断该目录是否存在
        //    if (Directory.Exists(dir) == false)
        //    {
        //        Directory.CreateDirectory(dir);//在工程下创建AssetBundles目录
        //    }

        //    BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.iOS);
        //    //刷新
        //    AssetDatabase.Refresh();
        //}

    }
 }

public class Platform
{
    public static string GetPlatformFolder(BuildTarget target)
    {
        switch (target)
        {
            case BuildTarget.Android:
                return "Android";
            case BuildTarget.iOS:
                return "IOS";
            case BuildTarget.StandaloneWindows:
            case BuildTarget.StandaloneWindows64:
                return "Windows";
            default:
                return null;
        }
    }
}